package poi.ui;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import com.ems.util.JFrameUtil;
import poi.action.ExcelAction;

public class PostroadUI {

	private static JFrame jFrame;

	private static JButton providerButton;
	private static JButton postroadButton;
	private static JTextArea resultArea;// 显示框
	private static JScrollPane jscrollPane;// 滚动条

	private static JFileChooser fc;// 选择文件夹控件。
	private static JButton selectFile;// 选择文件夹按钮。
	private static JLabel selectFileLebel;// 显示路径面板。
	private static String filesUrl;// 存放文件夹路径。

	private static GridBagConstraints gridbagArea;
	private static GridBagConstraints gridbagC;

	public static void createUI() {
		// 主层
		jFrame = new JFrame("省际出口流量");
		jFrame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		addComponentsToPane(jFrame.getContentPane());

		// 设置空间大小SIZE
		jFrame.setPreferredSize(new Dimension(400, 400));
		jFrame.setLocation(450, 150);
		jFrame.pack();
		jFrame.setVisible(true);

	}

	public static void addComponentsToPane(Container pane) {

		pane.setLayout(new GridBagLayout());

		gridbagArea = new GridBagConstraints();
		gridbagC = new GridBagConstraints();

		resultArea = new JTextArea("记录面板");
		resultArea.setEditable(true);
		// resultArea.setPreferredSize(new Dimension(100,100));

		jscrollPane = new JScrollPane(resultArea);
		jscrollPane
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		jscrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

		gridbagArea.fill = GridBagConstraints.HORIZONTAL;
		gridbagArea.ipady = 140;
		gridbagArea.ipadx = 300;
		gridbagArea.gridx = 0;
		gridbagArea.gridy = 0;
		pane.add(jscrollPane, gridbagArea);

		selectFile = new JButton("选择文件夹");
		gridbagC.fill = GridBagConstraints.HORIZONTAL;
		gridbagC.gridx = 0;
		gridbagC.gridy = 1;
		pane.add(selectFile, gridbagC);
		
		selectFile.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				filesUrl = JFrameUtil.chooseFile(fc);
				resultArea.setText(filesUrl);
			}
		});

		providerButton = new JButton("省际出口流量报表");
		gridbagC.fill = GridBagConstraints.HORIZONTAL;
		gridbagC.gridx = 0;
		gridbagC.gridy = 2;
		pane.add(providerButton, gridbagC);

		providerButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ExcelAction excelAction = new ExcelAction();
				excelAction.providerOutFlowDire("LL", filesUrl);
				String message = resultArea.getText() + "\n"
						+ "省际出口流量流向报表生成成功！文件名：省际出口流量流向.xls";
				resultArea.setText(message);
			}
		});

	}

}
